
var t = getApp(),
    e = t.requirejs("core"),
    a = t.requirejs("jquery"),
    s = t.requirejs("biz/diyform"),
    i = t.requirejs("biz/goodspicker");
t.requirejs("foxui");
Page({
    /**
     * 页面的初始数据
     */
    data: {
        price_up: null,
        list: [],
        alllist: [],
        tabid: 0,
        jg: null,
        screenlist: [],
        currentTab: [false, false, false, false, false, false],
        select: null,
        openid: null,
        selectid: null,
        params:{},
        lastcat: "",
        fromsearch: !1,
        searchRecords: [],
        areas: [],
        limits: !0,
        modelShow: !1,
        canload: !0,
        defaults: {
            keywords: "",
            isrecommand: "",
            ishot: "",
            isnew: "",
            isdiscount: "",
            issendfree: "",
            istime: "",
            cate: "",
            order: "",
            by: "desc",
            merchid: 0
        },
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        var openid = wx.getStorageSync('openid')
        that.setData({
            openid: openid
        })
        e.post("goods.tejia", {
            openid:that.data.openid,
            page:1,
            pagesize:10,
            zone_type:4
        }, ((e) => {
            that.setData({
                list: e.list,
                alllist: JSON.parse(JSON.stringify(e.list))
            })
        }))
        e.post("goods.get_category", {
            openid: that.data.openid
        }, ((e) => {
            that.setData({
                screenlist: e.allcategory.parent
            })
        }))
    },
    bindback: function () {
        var that=this
        that.setData({
            fromsearch:!that.data.fromsearch
        })
        // wx.switchTab({
        //   url: '/pages/sale/coupon/my/list/index',
        // })
    },
    delRecord: function () {
        this.setRecord(""), this.setData({
            fromsearch: !0
        });
    },
    bindfromsearch(){
        var that=this
        that.setData({
            fromsearch:!that.data.fromsearch
        })
    },
    bindnav: function (t) {
        var e = a.trim(t.currentTarget.dataset.text),
            s = this.data.defaults;
        s.keywords = e, this.setData({
            params: s,
            page: 1,
            fromsearch: !1
        }), this.getList(), this.setRecord(e);
    },
    getRecord: function () {
        var e = t.getCache("searchRecords");
        this.setData({
            searchRecords: e
        });
    },
    getList: function () {
        var t = this;
        e.post("goods.tejia",{
            openid:t.data.openid,
            page:1,
            pagesize:10,
            zone_type:4
        },((e)=>{
            t.setData({
                alllist:JSON.parse(JSON.stringify(e.list)),
                list:e.list
            })
        }))
        t.setData({
            loading: !0
        }), this.data.canload = !1, t.data.params.page = t.data.page, e.get("goods.tejia",t.data.params, function (e) {
                  console.log(t.data.params);
            var a = {
                loading: !1,
                count: e.total,
                show: !0
            };
            e.list || (e.list = []), e.list.length > 0 && (a.page = t.data.page + 1, a.list = t.data.list.concat(e.list),
                e.list.length < e.pagesize && (a.loaded = !0)), t.setData(a), t.data.canload = !0;
        });
    },
    setRecord: function (e) {
        if ("" != e) {
            var s = t.getCache("searchRecords");
            if (a.isArray(s)) {
                var i = [];
                for (var r in i.push(e), s) {
                    if (i.length > 20) break;
                    s[r] != e && null != s && "null" != s && i.push(s[r]);
                }
                s = i;
            } else(s = []).push(e);
            t.setCache("searchRecords", s);
        } else t.setCache("searchRecords", []);
        this.getRecord();
    },
    bindSearch: function (t) {
        t.target;
        this.setData({
            list: [],
            loading: !0,
            loaded: !1,
            tabid:0,
            jg:null
        });
        var e = a.trim(t.detail.value),
            s = this.data.defaults;
        "" != e ? (s.keywords = e, this.setData({
            page: 1,
            params: s,
            fromsearch: !1
        }), this.getList(), this.setRecord(e)) : (s.keywords = "", this.setData({
            page: 1,
            params: s,
            listorder: "",
            fromsearch: !1,
        }), this.getList());
    },
    bindFocus: function (t) {
        "" == a.trim(t.detail.value) && this.setData({
            fromsearch: !0
        });
    },
    changeMode: function () {
        "block" == this.data.listmode ? this.setData({
            listmode: ""
        }) : this.setData({
            listmode: "block",
        });
    },
    bindInput: function (t) {
        var e = a.trim(t.detail.value),
            s = this.data.defaults;
        s.keywords = "", s.order = this.data.params.order, s.by = this.data.params.by, "" == e && (this.setData({
            page: 1,
            list: [],
            loading: !0,
            loaded: !1,
            params: s,
            listorder: s.by,
            fromsearch: !0
        }), this.getRecord());
    },
    chengtab(e) {
        let tabid = e.currentTarget.dataset.id
        this.setData({
            tabid: tabid
        })
        if (tabid == 0) {
            var that = this
            that.setData({
                list: JSON.parse(JSON.stringify(this.data.alllist)), //深拷贝
                jg: null
            })
        } else if (tabid == 1) {
            var that = this
            var list = that.data.list
            var arr = list.sort(that.paixu('salesreal', false));
            list= arr
            that.setData({
                jg: null,
                list: list
            })
        } else if (tabid == 2) {
            let that = this
            var list = that.data.list
            var arr = list.sort(that.paixu('minprice', that.data.jg == 0));
            list = arr
            that.setData({
                jg: that.data.jg == 0 ? 1 : 0,
                list: list
            })
        }
    },
    confirm() {
        var that = this
        e.post("goods.tejia", {
            isrecommand: that.data.currentTab[0] ? 1 : null,
            isnew: that.data.currentTab[1] ? 1 : null,
            ishot: that.data.currentTab[2] ? 1 : null,
            isdiscount: that.data.currentTab[3] ? 1 : null,
            issendfree: that.data.currentTab[4] ? 1 : null,
            istime: that.data.currentTab[5] ? 1 : null,
            openid: that.data.openid,
            page:1,
            pagesize:10,
            zone_type:4,
            id: that.data.selectid
        }, ((e) => {
            console.log(e.list);
            that.setData({
                list: e.list,
                tabid: 3,
                jg: null
            })
        }))
    },
    clear_active() {
        var that = this
        that.setData({
            tabid: null,
            jg: null,
            tabid:0,
            currentTab: [false, false, false, false, false, false]
        })
    },
    select_change(e) {
        var that = this
        var id = e.currentTarget.dataset.selectid
        var cur = e.currentTarget.dataset.select
        console.log(id,cur);
        if (that.data.select == cur) {
            that.setData({
                select: null,
                selectid:null
            })
        } else {
            that.setData({
                select: cur,
                selectid: id
            })
        }
    },
    change_active(e) {
        var that = this
        //当前currentTab 数组赋值给arr 
        var arr = that.data.currentTab
        //当前选中下标
        var cur = e.currentTarget.dataset.current
        //如果当前选中和点击相同就取消
        arr[cur] = !arr[cur]
        that.setData({
            currentTab: arr
        })
        console.log(arr);
    },
    change: function (e) {
        var that = this
        var cur = e.currentTarget.dataset.current
        this.setData({
            currentTab: cur
        })
        if (cur == 0) {
            that.setData({
                price_up: null,
                list: JSON.parse(JSON.stringify(that.data.alllist))
            })
        } else if (cur == 1) {
            that.setData({
                price_up: null
            })
        } else if (cur == 2) {
            var list = that.data.list
            var arr = list.sort(that.paixu("minprice", that.data.price_up == 0))
            list.list = arr
            that.setData({
                price_up: that.data.price_up == 0 ? 1 : 0,
                list: list

            })
        } else if (cur == 3) {
            that.setData({
                price_up: null
            })
        }
    },
    // attr 需要验证的字符   rev   判定条件是ture 还是false
    paixu: function (attr, rev) {
        //第二个参数没有传递 默认升序排列
        if (rev == undefined) {
            rev = 1;
        } else {
            rev = (rev) ? 1 : -1;
        }
        //商品升序或者降序
        return function (a, b) {
            a = a[attr];
            b = b[attr];
            if (a < b) {
                return rev * -1;
            }
            if (a > b) {
                return rev * 1;
            }
            return 0;
        }
    },
    showFilter: function () {
        this.setData({
            isFilterShow: !this.data.isFilterShow
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})